<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise.prototype.catch()</title>
</head>
<body>
    <h1>Promise.prototype.catch()</h1>
    <h2>用于指定发生错误时的回调函数</h2>

    <script>
        // 基本用法
        const promise = new Promise((resolve, reject) => {
            throw new Error('测试错误');
        });

        promise.catch(error => {
            console.log(error.message); // "测试错误"
        });

        // catch方法的链式调用
        Promise.resolve()
            .then(() => {
                throw new Error('oh no!');
            })
            .catch(error => {
                console.log('捕获错误:', error.message);
                // 返回一个新的值，继续链式操作
                return '恢复正常';
            })
            .then(value => {
                console.log('继续执行:', value);
            });

        // catch之后还可能出错
        Promise.resolve()
            .then(() => {
                throw new Error('错误1');
            })
            .catch(error => {
                console.log('捕获错误1');
                throw new Error('错误2');
            })
            .catch(error => {
                console.log('捕获错误2');
            });
    </script>
</body>
</html> 